package com.beans;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.inject.Inject;
import javax.inject.Named;

import org.primefaces.component.ring.Ring;
import org.springframework.context.annotation.Scope;

import com.controllers.EmailController;
import com.controllers.EmployeeController;
import com.controllers.TaskController;
import com.model.Activity;
import com.model.Employee;
import com.model.Task;

@Named
@Scope(value="session")
public class NewTaskBean {

	private Ring ring;
	
	public Ring getRing() {
		return ring;
	}

	public void setRing(Ring ring) {
		this.ring = ring;
	}

	private Task myTask;

	public List<Employee> employees;

	public List<Activity> allActivities;

	private EmployeeController employeeController;

	private TaskController taskController;

	public NewTaskBean(){
		myTask = new Task();
		employees = employeeController.getEmployees();
		allActivities = taskController.getAllActivities();
	};
	
	@Inject
	public NewTaskBean(EmployeeController employeeController,
			TaskController taskController) {
		this.myTask = new Task();
		this.employeeController = employeeController;
		this.taskController = taskController;
		employees = employeeController.getEmployees();
		allActivities = taskController.getAllActivities();
	}
	
	public void save(ActionEvent actionEvent) {

		DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
		taskController.save(myTask);
		String email = myTask.getEmployee().getEmail();
		String name = myTask.getName();
		String userName = FacesContext.getCurrentInstance()
				.getExternalContext().getRemoteUser();
		try {
			Thread emailController = new Thread(new EmailController(email,
					name, dateFormat.format(myTask.getStartDate()),
					dateFormat.format(myTask.getFinishDate()), userName,
					myTask.getDescription(), 1));
			emailController.start();
		} catch (Exception exception) {
			System.out.println("No se pudo enviar mail para la tarea :"+myTask.getName());
			FacesContext.getCurrentInstance().addMessage(
					null,
					new FacesMessage(FacesMessage.SEVERITY_WARN,
							"No se pudo enviar mail para la tarea :"+myTask.getName(),
							"No se pudo enviar mail para la tarea :"+myTask.getName()));
		}
	//	ring.setRowIndex(3);
	//	cleanVariables();
	}

	public List<Activity> getAllActivities() {
		return allActivities;
	}

	public void setAllActivities(List<Activity> allActivities) {
		this.allActivities = allActivities;
	}

	public List<Employee> getEmployees() {
		return employees;
	}

	public void setEmployees(List<Employee> employees) {
		this.employees = employees;
	}

	public String getStartDateAsString() {
		DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
		return "";
		//return dateFormat.format(myTask.getActivity().getStartDate());
	}

	public String getFinishDateAsString() {
		DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
		//return dateFormat.format(myTask.getActivity().getFinishDate());
		return "";
	}

	public Task getMyTask() {
		return myTask;
	}

	public void setMyTask(Task myTask) {
		this.myTask = myTask;
	}
}
